﻿namespace Jsl.FxCop
{
    using System;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Collections.Generic;
using Microsoft.FxCop.Sdk;

    [TestClass]
    public class When_getting_AssemblyNode_from_Assembly
    {
        [TestMethod]
        public void The_correct_assembly_is_loaded()
        {
            var assembly = typeof(When_getting_AssemblyNode_from_Assembly).Assembly;
            var assemblyNode = assembly.GetAssemblyNode();

            var assemblyName = assembly.GetName();
            var assemblyNodeName = assemblyNode.GetAssemblyName();
            AssertEx.That(() => assemblyNodeName.CodeBase.Equals(assemblyName.CodeBase, StringComparison.OrdinalIgnoreCase));
            AssertEx.That(() => assemblyNodeName.FullName.Equals(assemblyName.FullName, StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void The_same_AssemblyNode_is_returned_for_the_same_Type()
        {
            var assemblyNode1 = typeof(When_getting_AssemblyNode_from_Assembly).Assembly.GetAssemblyNode();
            var assemblyNode2 = typeof(When_getting_AssemblyNode_from_Assembly).Assembly.GetAssemblyNode();

            AssertEx.That(() => object.ReferenceEquals(assemblyNode1, assemblyNode2));
        }

        [TestMethod]
        public void The_same_AssemblyNode_is_returned_for_different_Types()
        {
            var assemblyNode1 = typeof(When_getting_AssemblyNode_from_Assembly).Assembly.GetAssemblyNode();
            var assemblyNode2 = typeof(When_determining_the_compile_type_of_an_assembly).Assembly.GetAssemblyNode();

            AssertEx.That(() => object.ReferenceEquals(assemblyNode1, assemblyNode2));
        }
    }

    [TestClass]
    public class When_determining_the_compile_type_of_an_assembly
    {
        [TestMethod]
        public void Can_determine_assembly_was_compiled_in_debug_mode()
        {
            var assembly = new CompileHelper()
                .AddMethod("public void Xxx() { }")
                .Generate(true);

            AssertEx.That(() => assembly.IsDebugCompiled());
        }

        [TestMethod]
        public void Can_determine_assembly_was_compiled_in_release_mode()
        {
            var assembly = new CompileHelper()
                .AddMethod("public void Xxx() { }")
                .Generate(false);

            AssertEx.That(() => !assembly.IsDebugCompiled());
        }
    }
}